package at.socialconference.backend.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "t_talks")
public class Talk implements Insertable {

	public Talk() {}

	public Talk(at.socialconference.inter.model.Talk talk) {

		this.date = talk.getDate();
		this.description = talk.getDescription();
		this.from = talk.getFrom();
		this.id = talk.getId();
		this.place = new Place(talk.getPlace());
		for (at.socialconference.inter.model.User u : talk.getSpeakers()) {
			speakers.add(new User(u));
		}
		this.title = talk.getTitle();
		this.to = talk.getTo();
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "talkid")
	private Integer id;

	@Column(name = "title")
	private String title;

	@Column(name = "description")
	private String description;

	@Column(name = "date")
	private Date date;

	@Column(name = "time_from")
	private Date from;

	@Column(name = "time_to")
	private Date to;

	@ManyToOne
	@JoinColumn(name = "place")
	@LazyCollection(LazyCollectionOption.FALSE)
	private Place place;

	@ManyToMany
	@JoinTable(name = "t_speakers", joinColumns = { @JoinColumn(name = "talkid") }, inverseJoinColumns = { @JoinColumn(name = "speakerid") })
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<User> speakers = new ArrayList<User>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(Date to) {
		this.to = to;
	}

	public Place getPlace() {
		return place;
	}

	public void setPlace(Place place) {
		this.place = place;
	}

	public void setSpeakers(List<User> speakers) {
		this.speakers = speakers;
	}

	public List<User> getSpeakers() {
		return speakers;
	}

	public at.socialconference.inter.model.Talk generateInterTalk() {
		List<at.socialconference.inter.model.User> spea = new ArrayList<at.socialconference.inter.model.User>();
		for (User u : speakers)
			spea.add(u.generateInterUser(User.LEVEL_LOW));
		return new at.socialconference.inter.model.Talk(id, title, description, date, from, to,
				place.generateInterPlace(), spea);
	}

	public static List<at.socialconference.inter.model.Talk> getInterTalks(List<Talk> backendTalk){
		ArrayList<at.socialconference.inter.model.Talk> result = new ArrayList<at.socialconference.inter.model.Talk>();
		for (int i = 0; i < backendTalk.size(); i++) {
			at.socialconference.inter.model.Talk temp = backendTalk.get(i).generateInterTalk();
			result.add(i, temp);
		}

		return result;
	}

}
